import {
  ApiProperty,
  IntersectionType,
  OmitType,
  PartialType,
  PickType,
} from '@nestjs/swagger';
import { IsArray, IsEmail, IsInt, IsObject, IsString } from 'class-validator';
import { FindListDto } from 'src/modules/find-list.dto';
import { DepartmentObj } from '../department/department.dto';
import { PositionObj } from '../position/position.dto';

export class UserDto {
  @IsInt()
  @ApiProperty({ type: Number, description: 'id' })
  readonly id: number;

  @IsString()
  @ApiProperty({ type: String, description: '用户名', default: 'admin' })
  readonly user_name: string;

  @IsString()
  @ApiProperty({ type: String, description: '昵称', default: '张三' })
  readonly nick_name: string;

  @IsObject()
  @ApiProperty({
    type: PositionObj,
    description: '岗位',
    default: { id: 1 },
  })
  readonly position: PositionObj;

  @IsString()
  @ApiProperty({ type: String, description: '密码', default: '123456' })
  password: string;

  @IsString()
  @ApiProperty({ type: String, description: '性别', default: '0' })
  readonly sex: string;

  @IsString()
  @ApiProperty({ type: String, description: '状态', default: '0' })
  readonly status: string;

  @IsString()
  @ApiProperty({
    type: String,
    description: '删除标志（0代表存在 1代表删除）',
    default: '0',
  })
  readonly is_delete: string;

  @IsString()
  @ApiProperty({ type: String, description: '头像', default: null })
  readonly avatar: string;

  @IsEmail()
  @ApiProperty({
    type: String,
    description: '邮箱',
    default: null,
  })
  readonly email: string;

  @IsString()
  @ApiProperty({ type: String, description: '手机号', default: null })
  readonly phone: string;

  @IsString()
  @ApiProperty({
    type: Date,
    description: '出生日期',
    default: null,
  })
  readonly birthday: Date;

  @IsString()
  @ApiProperty({
    type: String,
    description: '备注',
    default: null,
  })
  readonly remark: string;

  @ApiProperty({ type: Date, description: '创建时间', default: null })
  readonly created_date: Date;

  @IsString()
  @ApiProperty({
    type: String,
    description: '创建人',
    default: null,
  })
  readonly created_by: string;

  @ApiProperty({ type: Date, description: '更新时间', default: null })
  readonly updated_date: Date;

  @IsString()
  @ApiProperty({
    type: String,
    description: '更新人',
    default: null,
  })
  readonly updated_by: string;

  @IsArray()
  @ApiProperty({
    type: Array,
    description: '角色',
    default: [1],
  })
  public roles: any[];

  @IsObject()
  @ApiProperty({
    type: DepartmentObj,
    description: '部门',
    default: { id: 1 },
  })
  public dept: DepartmentObj;
}

export class UpdateUserPwdDto {
  @IsInt()
  @ApiProperty({ type: Number, description: 'id' })
  readonly id: number;

  @IsString()
  @ApiProperty({ type: String, description: '旧密码', default: '123456' })
  oldPassword: string;

  @IsString()
  @ApiProperty({ type: String, description: '新密码', default: '123456' })
  password: string;
}
class UserQuery {
  @IsString()
  @ApiProperty({ type: String, description: '用户名', default: 'admin' })
  readonly user_name: string;

  @IsString()
  @ApiProperty({ type: String, description: '角色id', default: '1' })
  public roles: string;

  @IsString()
  @ApiProperty({ type: String, description: '部门id', default: '1' })
  public deptId: string;
}

export class FindUserListDto extends IntersectionType(
  PartialType(FindListDto),
  PartialType(UserQuery),
) {}

export class CreateUserDto extends PartialType(
  OmitType(UserDto, ['id', 'created_date', 'updated_date']),
) {}

export class UpdateUserDto extends PartialType(
  OmitType(UserDto, ['user_name', 'password', 'created_date', 'updated_date']),
) {}

export class ResetUserPwdDto extends PickType(UserDto, ['id', 'password']) {}
